// Package service @program: gateway
//@author: edte
//@create: 2021-12-29 14:50
package service

import (
	"gateway/client/chat"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type createroom struct {
	Token     string
	RoomId    int
	IsPrivate bool
}

func CreateRoom(ct *gin.Context) {
	json := createroom{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})

		return
	}
	room, err := chat.CreateChatRoom(json.Token, json.RoomId, json.IsPrivate)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, room)
		return
	}
	ct.JSON(http.StatusOK, room)
}

type joinRoom struct {
	Token  string
	RoomId int
}

func JoinRoom(ct *gin.Context) {
	json := joinRoom{}
	err := ct.BindJSON(&json)
	if err != nil {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	room, err := chat.JoinChatRoom(json.Token, json.RoomId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, room)
		return
	}
	ct.JSON(http.StatusOK, room)
}

type adduser struct {
	Token  string
	UserId int
	RoomId int
}

func AddUser(ct *gin.Context) {
	json := adduser{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.UserId == 0 || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	user, err := chat.MasterAddUser(json.Token, json.RoomId, json.UserId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, user)
		return
	}
	ct.JSON(http.StatusOK, user)
}

type quitRoom struct {
	Token  string
	RoomId int
}

func QuitRoom(ct *gin.Context) {
	json := quitRoom{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	room, err := chat.QuitRoom(json.Token, json.RoomId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, room)
		return
	}
	ct.JSON(http.StatusOK, room)
}

type queryMessage struct {
	Token  string
	RoomId int
}

func QueryMessage(ct *gin.Context) {
	json := queryMessage{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	message, err := chat.QueryMessage(json.Token, json.RoomId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, message)
		return
	}
	ct.JSON(http.StatusOK, message)
}

type sentMessage struct {
	Token   string
	RoomId  int
	Message string
}

func SentMessage(ct *gin.Context) {
	json := sentMessage{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	message1, err := chat.QueryMessage(json.Token, json.RoomId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, message1)
		return
	}
	ct.JSON(http.StatusOK, message1)
	message2, err := chat.SentMessage(json.Token, json.RoomId, json.Message)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, message2)
		return
	}
	ct.JSON(http.StatusOK, message2)
}
